#include "drawlabel.h"
#include "cal.h"

DrawLabel::DrawLabel(QWidget *parent) : QWidget(parent)
{
   //this->setStyleSheet(QString::fromUtf8("border:3px solid blue"));
   installEventFilter(this);
   update();
}

bool DrawLabel::eventFilter(QObject *obj, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        // 左键单击
        if(mouseEvent->button() == Qt::LeftButton)
        {
            qDebug() << mouseEvent->pos();
            pointCnt++;
            // 捕捉A点
            if(pointCnt==1)
            {
                isA = true;
                xa = mouseEvent->pos().x() - tx;
                ya = mouseEvent->pos().y() - ty;
                update();
            }
            // 捕捉B点
            else if(pointCnt==2)
            {
                isB = true;
                xb = mouseEvent->pos().x() - tx;
                yb = mouseEvent->pos().y() - ty;
                qDebug()<< xb << yb;
                update();
            }
            // 捕捉C点
            else if(pointCnt==3)
            {
                isC = true;
                xc = mouseEvent->pos().x() - tx;
                yc = mouseEvent->pos().y() - ty;
                qDebug()<< xc << yc;
                update();
            }
            // 其他情况需清空数据
            else
            {
                // 创建消息对话框
                QMessageBox * messageBox = new QMessageBox(this);
                // 设置对话框标题
                messageBox->setWindowTitle("消息对话框");
                // 设置对话框中的文本
                messageBox->setText("请点击右侧清除按钮，清除数据");
                // 设置对话框的图标
                messageBox->setIcon(QMessageBox::Warning);
                // 添加对话框内的按钮（按钮有QMessageBox自己生成）
                messageBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Save);

                // 模态对话框
                messageBox->setModal(true);
                messageBox->show();
                // messageBox->exec();
                //关闭窗口释放内存
                messageBox->setAttribute(Qt::WA_DeleteOnClose);
            }
        }
        else
        {
            return false;
        }

    }
    return QObject::eventFilter(obj, event);
}

void DrawLabel::paintEvent(QPaintEvent *)
{
    w = this->width();
    h = this->height();
    tx = w/2;
    ty = h/2;
    qDebug() << w << " " << h;
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);

    // 保存最初的 painter
    painter.save();
    painter.translate(tx, ty);

    if(pointCnt==0)
    {
        // 设置画刷颜色（填充颜色）
        QBrush brush;
        brush.setColor(QColor(237,243,247));
        brush.setStyle(Qt::Dense4Pattern);
        painter.setBrush(brush);
        // 设置画笔颜色
        QPen pen;
        pen.setColor(QColor(237,243,247));
        pen.setWidth(5);
        painter.setPen(pen);

        painter.drawRect(this->geometry());
    }
    drawDashLine(&painter, Qt::green, -tx, 0, tx, 0);
    drawDashLine(&painter, Qt::green, 0, -ty, 0, ty);

    if(isA)
    {
        // 发射信号到上层
        emit signalClicked(0, xa, ya);
        // 画点A，黑色
        drawDot(&painter, Qt::black, 6, xa, ya);
    }
    if(isB)
    {
        // 发射信号到上层
        emit signalClicked(1, xb, yb);
        // 画点B， 黑色
        drawDot(&painter, Qt::black, 6, xb, yb);
        // 画线AB， 黑色
        drawLine(&painter, Qt::black, xa, ya, xb, yb);
    }
    if(isC)
    {
        // 发射信号到上层
        emit signalClicked(2, xc, yc);
        // 画点C，黑色
        drawDot(&painter,Qt::black, 6, xc, yc);
        // 画线BC AC 黑色
        drawLine(&painter, Qt::black, xb, yb, xc, yc);
        drawLine(&painter, Qt::black, xc, yc, xa, ya);
        if (moreThan120(xa,ya,xb,yb,xc,yc))
        {
            // 发射信号到上层
            emit signalClicked(7, xa, ya);
            // 画费马点， 红色
            drawDot(&painter,Qt::red, 8, xa, ya);
            qDebug()<<xa<<" "<<ya<<endl;
        }
        else if (moreThan120(xb,yb,xa,ya,xc,yc))
        {
            // 发射信号到上层
            emit signalClicked(8, xb, yb);
            // 画费马点， 红色
            drawDot(&painter,Qt::red, 8, xb, yb);
            qDebug()<<xb<<" "<<yb<<endl;
        }
        else if (moreThan120(xc,yc,xa,ya,xb,yb))
        {
            // 发射信号到上层
            emit signalClicked(9, xc, yc);
            // 画费马点， 红色
            drawDot(&painter,Qt::red, 8, xc, yc);
            qDebug()<<xc<<" "<<yc<<endl;
        }
        else
        {
            // 计算A1 B1 C1
            C1=getAnotherPoint(Point(xa,ya),Point(xb,yb),Point(xc,yc));
            A1=getAnotherPoint(Point(xc,yc),Point(xb,yb),Point(xa,ya));
            B1=getAnotherPoint(Point(xa,ya),Point(xc,yc),Point(xb,yb));
            // 发送信号到上层
            emit signalClicked(3, A1.x, A1.y);
            emit signalClicked(4, B1.x, B1.y);
            emit signalClicked(5, C1.x, C1.y);

            // 画点A1 和 等边三角形A1BC
            drawDot(&painter, Qt::blue, 6, A1.x, A1.y);
            drawDashLine(&painter,Qt::blue, A1.x, A1.y, xb, yb);
            drawDashLine(&painter, Qt::blue, A1.x, A1.y, xc, yc);
            //QThread::msleep(2000);
            // 画点C1 和 等边三角形ABC1
            drawDot(&painter,Qt::blue, 6, C1.x, C1.y);
            drawDashLine(&painter, Qt::blue, C1.x, C1.y, xa, ya);
            drawDashLine(&painter, Qt::blue, C1.x, C1.y, xb, yb);
            //QThread::msleep(2000);
            // 画点B1 和 等边三角形AB1C
            drawDot(&painter, Qt::blue, 6, B1.x, B1.y);
            drawDashLine(&painter, Qt::blue, B1.x, B1.y, xa, ya);
            drawDashLine(&painter, Qt::blue, B1.x, B1.y, xc, yc);

            // 计算费马点
            R=getCrossPoint(Point(xa,ya),A1,Point(xc,yc),C1);
            // 发射信号到上层
            emit signalClicked(6, R.x, R.y);
            // 画线 红色
            drawDashLine(&painter, Qt::red, B1.x, B1.y, xb, yb);
            drawDashLine(&painter, Qt::red, C1.x, C1.y, xc, yc);
            drawDashLine(&painter, Qt::red, A1.x, A1.y, xa, ya);

            // 画费马点 红色 8号
            drawDot(&painter,Qt::red, 8, R.x, R.y);
            qDebug() <<R.x<<" "<<R.y<<endl;
        }
    }
//    painter.drawPoint(100, 100);
//    painter.drawPoint(200, 200);
//    painter.drawPoint(300, 300);
//    painter.drawPoint(400, 400);
//    painter.drawPoint(500, 500);
    painter.restore();
}

void DrawLabel::drawDot(QPainter *painter, QColor color, int size, int x, int y)
{
    // 设置画笔颜色
    QPen pen;
    // 设置颜色
    pen.setColor(color);
    // 设置大小
    pen.setWidth(size);
    // 加载画笔
    painter->setPen(pen);
    // 画点
    painter->drawPoint(QPoint(x,y));
}
void DrawLabel::drawLine(QPainter *painter, QColor color, int startX, int startY, int endX, int endY)
{
    // 设置画笔颜色
    QPen pen;
    pen.setColor(color);
    pen.setWidth(2);
    painter->setPen(pen);
    // 路径句柄
    QPainterPath pt;
    // 移动到点
    pt.moveTo(startX, startY);
    // 画直线
    pt.lineTo(endX, endY);
    // 加载路径
    painter->drawPath(pt);
}

void DrawLabel::drawDashLine(QPainter *painter, QColor color, int startX, int startY, int endX, int endY)
{
    // 设置画笔颜色
    QPen pen;
    pen.setColor(color);
    pen.setWidth(2);
    pen.setStyle(Qt::DashLine);
    painter->setPen(pen);
    // 路径句柄
    QPainterPath pt;
    pt.moveTo(startX, startY);
    pt.lineTo(endX, endY);
    painter->drawPath(pt);
}

void DrawLabel::clearPoint()
{
    // 清除数据
    qDebug() <<"clear draw";
    isA = false;
    isB = false;
    isC = false;
    pointCnt = 0;
    update();
}
